import type { App } from "vue";
import type { RouteLocationNormalized } from "vue-router";
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import { routes } from "@/router/modules";
import { createRouterGuard } from "@/router/guard";

export const router = createRouter({
  // 路由模式选择
  history:
    process.env.VITE_HASH_ROUTE === "Y"
      ? createWebHashHistory(process.env.VITE_BASE_URL)
      : createWebHistory(process.env.VITE_BASE_URL),
  routes: routes,
});

// 判断当前跳转的路由是否在router.getRoutes()里面
export const hasNecessaryRoute = (to: RouteLocationNormalized): boolean =>
  router.getRoutes().findIndex(r => r.path === to.path) !== -1;

/**
 * setup vue router. - [安装vue路由]
 */
export async function setupRouter(app: App) {
  app.use(router);
  createRouterGuard(router);
  await router.isReady();
}
